package cn.itcast.nems.order.web.controller.b;

import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.ic.common.exception.v2.bean.BizStatusEnum;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.manager.scrm.SCRMManager;
import cn.itcast.nems.manager.scrm.dto.CollegeActivity;
import cn.itcast.nems.manager.scrm.dto.CustomerInfo;
import cn.itcast.nems.order.web.vo.CollegeActivityQueryVO;
import cn.itcast.nems.order.web.vo.CustomerQueryVO;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/b")
@Slf4j
@RequiredArgsConstructor
public class CustomerController {

    private final SCRMManager scrmManager;

    @GetMapping("/customer/{customerId}")
    public ResponseEntity<CustomerInfo> customerInfo(@PathVariable("customerId") int customerId) {
        final CustomerInfo customerInfo = this.scrmManager.findCustomerInfo(customerId);
        return ResponseEntity.ok(customerInfo);
    }

    /**
     * 客户分页查询
     *
     * @param pageRequest 分页查询条件
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.manager.scrm.dto.CustomerInfo>
     * @author 高昌露
     * @since 2023/12/6 15:59
     */
    @PostMapping("/customer/page/list")
    public ResponseEntity<Page<CustomerInfo>> customerPageList(@RequestBody BasePageRequest<CustomerQueryVO> pageRequest) {
        Assert.notNull(pageRequest.getCondition(), BizExceptionProducer.badRequest("查询条件不可为空"));
        return ResponseEntity.ok(
                this.scrmManager.customerPageList(
                        this.buildPageRequestJsonString(
                                pageRequest.getPageSize(),
                                pageRequest.getPageNumber(),
                                pageRequest.getCondition()
                        )
                )
        );
    }

    /**
     * 院校活动分页查询
     *
     * @param pageRequest 分页查询条件
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.manager.scrm.dto.CollegeActivity>
     * @author 高昌露
     * @since 2023/12/6 15:59
     */
    @PostMapping("/college/activity/page/list")
    public ResponseEntity<Page<CollegeActivity>> collegeActivityPageList(@RequestBody BasePageRequest<CollegeActivityQueryVO> pageRequest) {
        Assert.notNull(pageRequest.getCondition(), BizExceptionProducer.badRequest("查询条件不可为空")) ;
        Assert.notNull(pageRequest.getCondition().getActivityName(), BizExceptionProducer.badRequest(BizStatusEnum.BAD_REQUEST.toString(), "查询条件:%s不可为空", "活动名称")) ;
        return ResponseEntity.ok(
                this.scrmManager.collegeActivityPageList(
                        this.buildPageRequestJsonString(
                                pageRequest.getPageSize(),
                                pageRequest.getPageNumber(),
                                pageRequest.getCondition()
                        )
                )
        );
    }

    /**
     * 构建SCRM分页请求体
     *
     * @param pageSize   每页条数
     * @param pageNumber 页码
     * @param condition  条件
     * @return 分页请求参数
     */
    private String buildPageRequestJsonString(Integer pageSize, Integer pageNumber, Object condition) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("where", condition);
        jsonObject.put("pageIndex", pageNumber);
        jsonObject.put("pageSize", pageSize);
        return jsonObject.toJSONString();
    }
}
